from django.shortcuts import render_to_response
from django.template import RequestContext

from apps.news.models import GetAnnouncements, GetEvents, Event
from apps.activities.models import Activity
from apps.participants.models import CourseParticipant

from models import *

import logging
import datetime
import calendar

logger = logging.getLogger('pet.apps.course.views')

def index(request, course=None):
    TheCourse = Course.find_or_404(course)
    TheCourseParticipant = CourseParticipant.find(course,user=request.user)
    public_only = True
    if request.user.is_superuser or TheCourseParticipant: public_only = False

    AnnouncementsToShow = GetAnnouncements(TheCourse.course_id,5)
    template = "course/index.html"
    if TheCourse.course_id == '__default': template = "course/default_index.html"
    
    today = datetime.date.today()
    month = today.month
    year = today.year
    if request.method == "GET":
        if 'month' in request.GET:
            try:
                month = int(request.GET['month'])
            except ValueError: pass
        if 'year' in request.GET:
            try:
                year = int(request.GET['year'])
            except ValueError: pass
    Events = []
    last_day_of_month = calendar.monthrange(year, month)[1]
    
    #Add Events
    EventsToShow = ([{'date':item.date,'object':item,'title':item.title,
        'link':item.get_link(TheCourse.course_id) if not item.calendar_only else '','description':item.description,'css_class':'calendar_event' if item.calendar_only else 'event'} for item in
        Event.objects.filter(course__contains=TheCourse.course_id,date__gte=datetime.date(year,month,1), date__lte=datetime.date(year,month,last_day_of_month))])
    Events.extend(EventsToShow)
    
    #Add Activity Start Announcements
    ActivityStarts = ([{'date':item.start_date,'object':item,'title':"%s %s"%(item.title,'Assigned' if item.questions.all() else 'Posted'),
        'link':item.get_link(TheCourse.course_id),'description':'Click to go to this assignment' if item.questions.all() else 'Click to go to this page', 'css_class':'activity_started' if item.questions.all() else 'activity_posted'} for item in
        Activity.objects.filter(course__contains=TheCourse.course_id,
                                start_date__gte=datetime.datetime(year,month,1,0,0,0),
                                start_date__lte=datetime.datetime(year,month,last_day_of_month,23,59,59))
        if item.is_public or (item.is_public == False and public_only == False)])
    Events.extend(ActivityStarts)
    
    #Add Activity End Announcements
    ActivityEnds = ([{'date':item.end_date,'object':item,'title':"%s Due %s"%(item.title,item.end_date.strftime("%I:%M %p") if TheCourse.event_calendar_show_time_due else ""),
        'link':item.get_link(TheCourse.course_id),'description':'Click to go to this assignment', 'css_class':'activity_ended'} for item in
        Activity.objects.filter(course__contains=TheCourse.course_id,
                                end_date__gte=datetime.datetime(year,month,1,0,0,0),
                                end_date__lte=datetime.datetime(year,month,last_day_of_month,23,59,59))
        if item.questions.all() and (item.is_public or (item.is_public == False and public_only == False))])
    Events.extend(ActivityEnds)
    
    return render_to_response(template, {'announcements':AnnouncementsToShow, 'events':Events, 'year':year, 'month':month,
                        'permissions':{'add_announcement_allowed':request.user.has_perm('news.add_announcement'), 'add_event_allowed':request.user.has_perm('news.add_event')}},
                        context_instance = RequestContext(request))
